/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.material;

import org.openaecfm.model.material.DirectionSenseEnum;
import org.openaecfm.model.material.LayerSetDirectionEnum;
import org.openaecfm.model.material.MaterialLayerSet;
import org.openaecfm.model.material.MaterialLayerSetUsage;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class MaterialLayerSetUsageIO extends
		AbstractEntityInstanceInputImpl<MaterialLayerSetUsage> implements
		IEntityInstanceOutput<MaterialLayerSetUsage> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(MaterialLayerSetUsage pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCMATERIALLAYERSETUSAGE", appDataManager.getName(pojo));

		// Set ForLayerSet
		EntityInstanceNameParameter forlayerset = new EntityInstanceNameParameter(
				appDataManager.getName(pojo.getForLayerSet()));
		ei.addParameter(forlayerset);

		// Set LayerSetDirection
		EnumerationParameter direc = new EnumerationParameter(
				pojo.getLayerSetDirection());
		ei.addParameter(direc);

		// Set DirectionSense
		EnumerationParameter sense = new EnumerationParameter(
				pojo.getDirectionSense());
		ei.addParameter(sense);

		// Set OffsetFromReferenceLine
		RealParameter offset = new RealParameter(
				pojo.getOffsetFromReferenceLine());
		ei.addParameter(offset);

		// Set ReferenceExtent
		if (pojo.getReferenceExtent() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getReferenceExtent()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected MaterialLayerSetUsage doCreateObject() {
		return new MaterialLayerSetUsage();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			MaterialLayerSetUsage pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ForLayerSet
		EntityInstanceNameParameter forlayerset = (EntityInstanceNameParameter) sei
				.getParameter(0);
		pojo.setForLayerSet((MaterialLayerSet) appDataManager
				.getObject(forlayerset.toInstanceName()));
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			MaterialLayerSetUsage pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set LayerSetDirection
		EnumerationParameter direc = (EnumerationParameter) sei.getParameter(1);
		pojo.setLayerSetDirection(LayerSetDirectionEnum.valueOf(direc
				.toEnumString()));

		// Set DirectionSense
		EnumerationParameter sense = (EnumerationParameter) sei.getParameter(2);
		pojo.setDirectionSense(DirectionSenseEnum.valueOf(sense.toEnumString()));

		// Set OffsetFromReferenceLine
		RealParameter offset = (RealParameter) sei.getParameter(3);
		pojo.setOffsetFromReferenceLine(offset.toDouble());

		// Set ReferenceExtent
		Parameter referenceExtent = sei.getParameter(4);
		if (referenceExtent.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) referenceExtent;
			pojo.setReferenceExtent(r.toDouble());
		}
	}
}
